package com.ztstech.matisse_android_plugin.util;


import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * Created by zhiyuan on 2017/7/28.
 * view相关工具
 */

public class ViewUtils {

    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);

    }

    public static int sp2px(Context context,float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    /**
     * 获得屏幕高度
     */
    @SuppressWarnings("deprecation")
    public static int getPhoneHeight(Context ctx){
        if (ctx == null){
            return 0;
        }
        WindowManager wm = (WindowManager) ctx
                .getSystemService(Context.WINDOW_SERVICE);

        int height = wm.getDefaultDisplay().getHeight();
        return height;
    }

    /**
     * 获得屏幕高宽比
     */
    public static float getPhoneScale(Context context){
        return (float) getPhoneHeight(context) / (float) getPhoneWidth(context);
    }
    /**
     * 获得屏幕宽度
     */
    @SuppressWarnings("deprecation")
    public static int getPhoneWidth(Context ctx){
        if (ctx == null){
            return 0;
        }
        WindowManager wm = (WindowManager) ctx
                .getSystemService(Context.WINDOW_SERVICE);

        int width = wm.getDefaultDisplay().getWidth();
        return width;
    }


}
